package excel.countryMedicine;

import java.io.File;
import java.io.FileOutputStream;
import java.util.ArrayList;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import org.apache.log4j.Logger;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.xssf.streaming.SXSSFWorkbook;

import po.MyObject;
import util.AbstractExl;
import util.Conts;

public class ImoprtInstrument extends AbstractExl {
	
	private static final Logger LOG = Logger.getLogger(ImoprtInstrument.class);
	private static String fileName = "进口医疗器械";
	private static int start = 1;
	private static int rowNum = Conts.PAGE_IMPORT_INSTRUMENT;
	private static String filePath = Conts.HTM_ROOT_PATH+fileName+"/";
	private static String outputFolder = Conts.EXL_ROOT_PATH;
	private static String regex = "<td bgcolor=\"#eaeaea\" style=\"text-align:right\" width=17% nowrap=\"true\">.*?</td>.*?"+
			"<td bgcolor=\"#eaeaea\" width=83%>(.*?)</td>.*?</tr>.*?"+
			"<tr>.*?"+
			"<td bgcolor=\"#ffffff\" style=\"text-align:right\" width=17% nowrap=\"true\">.*?</td>.*?"+
			"<td bgcolor=\"#ffffff\" width=83%>(.*?)</td>.*?</tr>.*?"+
			"<tr>.*?"+
			"<td bgcolor=\"#eaeaea\" style=\"text-align:right\" width=17% nowrap=\"true\">.*?</td>.*?"+
			"<td bgcolor=\"#eaeaea\" width=83%>(.*?)</td>.*?</tr>.*?"+
			"<tr>.*?"+
			"<td bgcolor=\"#ffffff\" style=\"text-align:right\" width=17% nowrap=\"true\">.*?</td>.*?"+
			"<td bgcolor=\"#ffffff\" width=83%>(.*?)</td>.*?</tr>.*?"+
			"<tr>.*?"+
			"<td bgcolor=\"#eaeaea\" style=\"text-align:right\" width=17% nowrap=\"true\">.*?</td>.*?"+
			"<td bgcolor=\"#eaeaea\" width=83%>(.*?)</td>.*?</tr>.*?"+
			"<tr>.*?"+
			"<td bgcolor=\"#ffffff\" style=\"text-align:right\" width=17% nowrap=\"true\">.*?</td>.*?"+
			"<td bgcolor=\"#ffffff\" width=83%>(.*?)</td>.*?</tr>.*?"+
			"<tr>.*?"+
			"<td bgcolor=\"#eaeaea\" style=\"text-align:right\" width=17% nowrap=\"true\">.*?</td>.*?"+
			"<td bgcolor=\"#eaeaea\" width=83%>(.*?)</td>.*?</tr>.*?"+
			"<tr>.*?"+
			"<td bgcolor=\"#ffffff\" style=\"text-align:right\" width=17% nowrap=\"true\">.*?</td>.*?"+
			"<td bgcolor=\"#ffffff\" width=83%>(.*?)</td>.*?</tr>.*?"+
			"<tr>.*?"+
			"<td bgcolor=\"#eaeaea\" style=\"text-align:right\" width=17% nowrap=\"true\">.*?</td>.*?"+
			"<td bgcolor=\"#eaeaea\" width=83%>(.*?)</td>.*?</tr>.*?"+
			"<tr>.*?"+
			"<td bgcolor=\"#ffffff\" style=\"text-align:right\" width=17% nowrap=\"true\">.*?</td>.*?"+
			"<td bgcolor=\"#ffffff\" width=83%>(.*?)</td>.*?</tr>.*?"+
			"<tr>.*?"+
			"<td bgcolor=\"#eaeaea\" style=\"text-align:right\" width=17% nowrap=\"true\">.*?</td>.*?"+
			"<td bgcolor=\"#eaeaea\" width=83%>(.*?)</td>.*?</tr>.*?"+
			"<tr>.*?"+
			"<td bgcolor=\"#ffffff\" style=\"text-align:right\" width=17% nowrap=\"true\">.*?</td>.*?"+
			"<td bgcolor=\"#ffffff\" width=83%>(.*?)</td>.*?</tr>.*?"+
			"<tr>.*?"+
			"<td bgcolor=\"#eaeaea\" style=\"text-align:right\" width=17% nowrap=\"true\">.*?</td>.*?"+
			"<td bgcolor=\"#eaeaea\" width=83%>(.*?)</td>.*?</tr>.*?"+
			"<tr>.*?"+
			"<td bgcolor=\"#ffffff\" style=\"text-align:right\" width=17% nowrap=\"true\">.*?</td>.*?"+
			"<td bgcolor=\"#ffffff\" width=83%>(.*?)</td>.*?</tr>.*?"+
			"<tr>.*?"+
			"<td bgcolor=\"#eaeaea\" style=\"text-align:right\" width=17% nowrap=\"true\">.*?</td>.*?"+
			"<td bgcolor=\"#eaeaea\" width=83%>(.*?)</td>.*?</tr>.*?"+
			"<tr>.*?"+
			"<td bgcolor=\"#ffffff\" style=\"text-align:right\" width=17% nowrap=\"true\">.*?</td>.*?"+
			"<td bgcolor=\"#ffffff\" width=83%>(.*?)</td>.*?</tr>.*?"+
			"<tr>.*?"+
			"<td bgcolor=\"#eaeaea\" style=\"text-align:right\" width=17% nowrap=\"true\">.*?</td>.*?"+
			"<td bgcolor=\"#eaeaea\" width=83%>(.*?)</td>.*?</tr>.*?"+
			"<tr>.*?"+
			"<td bgcolor=\"#ffffff\" style=\"text-align:right\" width=17% nowrap=\"true\">.*?</td>.*?"+
			"<td bgcolor=\"#ffffff\" width=83%>(.*?)</td>.*?</tr>.*?"
			+"id is 【(.*?)】.*?";

	private void writeExl(List<MyObject> list) {
		File folder = new File(outputFolder);
		if (!folder.exists()) folder.mkdirs();
		FileOutputStream fOut = null;
		try {
			Workbook workbook = new SXSSFWorkbook(2000);
			Sheet sheet = workbook.createSheet("Sheet1");
			Row row = null;
			fOut = new FileOutputStream(outputFolder+fileName+".xlsx");
				for (int i = 0; i < list.size(); i++) {
					row = sheet.createRow(i);
					row.createCell((short)0).setCellValue(list.get(i).getObj1().trim());
					row.createCell((short)1).setCellValue(list.get(i).getObj2().trim());
					row.createCell((short)2).setCellValue(list.get(i).getObj3().trim());
					row.createCell((short)3).setCellValue(list.get(i).getObj4().trim());
					row.createCell((short)4).setCellValue(list.get(i).getObj5().trim());
					row.createCell((short)5).setCellValue(list.get(i).getObj6().trim());
					row.createCell((short)6).setCellValue(list.get(i).getObj7().trim());
					row.createCell((short)7).setCellValue(list.get(i).getObj8().trim());
					row.createCell((short)8).setCellValue(list.get(i).getObj9().trim());
					row.createCell((short)9).setCellValue(list.get(i).getObj10().trim());
					row.createCell((short)10).setCellValue(list.get(i).getObj11().trim());
					row.createCell((short)11).setCellValue(list.get(i).getObj12().trim());
					row.createCell((short)12).setCellValue(list.get(i).getObj13().trim());
					row.createCell((short)13).setCellValue(list.get(i).getObj14().trim());
					row.createCell((short)14).setCellValue(list.get(i).getObj15().trim());
					row.createCell((short)15).setCellValue(list.get(i).getObj16().trim());
					row.createCell((short)16).setCellValue(list.get(i).getObj17().trim());
					row.createCell((short)17).setCellValue(list.get(i).getObj18().trim());
					row.createCell((short)18).setCellValue(list.get(i).getObj19());
//					LOG.info("It had "+i+" rows");
				}
			workbook.write(fOut);
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			if (null != fOut) {
				try {
					fOut.close();
					LOG.info(fileName +" success export excel ! And the excel had " + list.size() + " rows.");
				} catch (Exception e) {
					e.printStackTrace(System.err);
				}
			}
		}
	}
	
	private void excute() {
		try {
			List<MyObject> list = new ArrayList<MyObject>();;
			StringBuffer content = null;
			MyObject o = new MyObject();
			o.setObj1("APPROVE_CODE");
			o.setObj2("FACTORY_NAME");
			o.setObj3("FACTORY_NAME_EN");
			o.setObj4("FACTORY_ADDRESS");
			o.setObj5("FACTORY_ADDRESS2");
			o.setObj6("FACTORY_COUNTRY");
			o.setObj7("PRODUCT_NAME");
			o.setObj8("PRODUCT_NAME_EN");
			o.setObj9("INST_SPEC");
			o.setObj10("PRODUCT_STANDARD");
			o.setObj11("PRODUCT_INFO");
			o.setObj12("PRODUCT_LIMIT");
			o.setObj13("REG_AGENT");
			o.setObj14("AFTER_SALE_SERVICE_ORG");
			o.setObj15("APPROVE_DATE");
			o.setObj16("VALID_DATE");
			o.setObj17("II_DESC");
			o.setObj18("CHANGE_DATE");
			o.setObj19("II_ID");
			list.add(o);
			for (int i = start; i <= rowNum; i++) {
				content = readHtm(filePath+i+".htm");
				Pattern pattern = Pattern.compile(regex);
				Matcher matcher = pattern.matcher(content);
				while (matcher.find()) {
					o = new MyObject();
					o.setObj1(matcher.group(1).trim());
					o.setObj2(matcher.group(2).replace("", "").trim());
					o.setObj3(matcher.group(3).trim());
					o.setObj4(matcher.group(4).trim());
					o.setObj5(matcher.group(5).trim());
					o.setObj6(matcher.group(6).trim());
					o.setObj7(matcher.group(7).trim());
					o.setObj8(matcher.group(8).trim());
					o.setObj9(matcher.group(9).trim());
					o.setObj10(matcher.group(10).trim());
					o.setObj11(matcher.group(11).trim().replace("<br>", ""));
					o.setObj12(matcher.group(12).trim());
					o.setObj13(matcher.group(13).trim());
					o.setObj14(matcher.group(14).trim());
					o.setObj15(matcher.group(15).trim().replace(".", "-").replace("<br>", ""));
					o.setObj16(matcher.group(16).trim().replace(".", "-").replace("-  -", ""));
					o.setObj17(matcher.group(17).trim());
					o.setObj18(matcher.group(18).trim().replace(".", "-").replace("<br>", "").replace("-  -", ""));
					o.setObj19(matcher.group(19).trim());
					list.add(o);
					LOG.info("file is NO." + i);
				}
			}
				writeExl(list);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	public static void main(String[] args) {
		ImoprtInstrument instance = new ImoprtInstrument();
		instance.excute();
	}
}